package com.zhaotianhao.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaotianhao.mapper.CartsMapper;
import com.zhaotianhao.mapper.OrderItemMapper;
import com.zhaotianhao.mapper.OrderMapper;
import com.zhaotianhao.pojo.Carts;
import com.zhaotianhao.pojo.Order;
import com.zhaotianhao.pojo.OrderItem;
import com.zhaotianhao.service.OrderService;
import com.zhaotianhao.util.SnowflakeConfig;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author Z-7
 * @since 2022-03-03 17:54:08
 */
@DubboService
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CartsMapper cartsMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(String id,Integer uid) {
        return this.orderMapper.queryById(id,uid);
    }

    /**
     * 分页查询
     *
     * @param order 筛选条件
     * @param pageNum 起始页数
     * @param pageSize 分页偏移量
     * @return 查询结果
     */
    @Override
    public PageInfo<Order> queryByPage(Order order,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(this.orderMapper.queryAll(order));
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 影响行数
     */
    @Override
    public int insert(Order order) {
        return this.orderMapper.insert(order);
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 影响行数
     */
    @Override
    public int update(Order order) {
        return this.orderMapper.update(order);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.orderMapper.deleteById(id) > 0;
    }

    /**
     * 创建订单以及详情
     *
     * @param goodsId
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public Order createOrder(Integer[] goodsId, Integer userId) {
        String snowflake = SnowflakeConfig.snowflakeIdStr();
        List<Carts> carts = cartsMapper.queryByIds(goodsId);
        List<OrderItem> items = new ArrayList<>();
        double totalMoney = 0;
        // 创建订单详情
        for (Carts cart : carts) {
            OrderItem orderItem = new OrderItem();
            orderItem.setGoodsId(cart.getGoodsId()).setOrderId(snowflake).setPrice(cart.getGoodsPrice()).setBuyNum(cart.getBuyNum()).setGoodsName(cart.getGoodsName());
            items.add(orderItem);
            orderItemMapper.insert(orderItem);
            totalMoney += cart.getBuyNum() * cart.getGoodsPrice();
        }
        Order order = new Order();
        order.setId(snowflake).setUserId(userId).setCreateTime(DateUtil.date()).setPrice(totalMoney).setFactPrice(totalMoney).setOrderItems(items);
        int i = orderMapper.insert(order);
        if(i > 0){
            return order;
        }
        return null;
    }

    /**
     * 改变支付支付状态
     *
     * @param id
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public int updateOrderStatus(String id, int uid) {
        int i = orderMapper.updateStatusByIdInt(id, uid);
        Integer[] goodsId = orderItemMapper.queryByorderId(id);
        for (Integer gid : goodsId) {
            // 删除购物车
            cartsMapper.deleteCarts(uid,gid);
        }
        return i;
    }

    /**
     * 获取未支付的订单
     *
     * @param i
     * @return
     */
    @Override
    public List<Order> listUnPay(int time) {
        return this.orderMapper.listUnPay(time);
    }
}
